//
//  LApplyRefundContentCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/1.
//

#import "HNSH_ApplyRefundContentCell.h"

@interface HNSH_ApplyRefundContentCell ()<UITextViewDelegate>
/** */
@property (nonatomic, strong)UIView * bgView;
/** */
@property (nonatomic, strong)UITextView * textView;
/** */
@property (nonatomic, strong)UILabel * countLabel;
@end

@implementation HNSH_ApplyRefundContentCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor appBGColor];
        [self configUI];
    }
    return self;
}

- (void)configUI {
    [self addAllViews];
    [self addAllLayout];
}

//MARK: Cycle Methods
- (void)addAllViews{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.textView];
    [self.bgView addSubview:self.countLabel];
}

- (void)addAllLayout{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.height.mas_equalTo(self.contentView);
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(14);
        make.bottom.mas_equalTo(-16);
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(-16);
        make.height.mas_equalTo(22);
    }];
}

//MARK: UITextViewDelegate Methods
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length >= 300) {
        textView.text = [textView.text substringToIndex:300];
    }
    self.countLabel.text = [NSString stringWithFormat:@"%ld/300", textView.text.length];
    if (self.changeBlock) {
        self.changeBlock(textView.text);
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"请输入退货的问题描述";
        textView.textColor = [UIColor appGrayLabelColor];
    } else {
        textView.textColor = [UIColor appBlackLabelColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"请输入退货的问题描述"]){
        textView.text=@"";
        textView.textColor=[UIColor appBlackLabelColor];
    }
}

//MARK: Setter Methods
- (void)setDic:(NSDictionary *)dic{
    _dic = dic;
    
    self.textView.placeholder = dic[@"place"];
}

//MARK: Getter Methods
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 4;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UITextView *)textView{
    if (!_textView) {
        _textView = [UITextView new];
        _textView.backgroundColor = [UIColor clearColor];
        _textView.font = [UIFont systemFontOfSize:16];
        _textView.delegate = self;
        _textView.text = @"请输入退货的问题描述";
        _textView.textColor = [UIColor appGrayLabelColor];
    }
    return _textView;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [UILabel new];
        _countLabel.textColor = [UIColor appGrayLabelColor];
        _countLabel.font = [UIFont systemFontOfSize:16];
        _countLabel.textAlignment = NSTextAlignmentRight;
        _countLabel.text = @"0/300";
    }
    return _countLabel;
}

@end
